/**
 * 自动生成 WxEventAward
 * 
 * Public/js/mp/wx_event_award.js
 * Zix @ 2015-07-06
 */

var WxEventAward = {
  config: {
  },
  init: function (config) {
    var _this = this;
    $.extend(_this.config, config);
    Layout.set_menu_select(_this.config.menu_id);
    $('.dtp').datetimepicker(DTP_DATE_OPTION);
    loading();
    _this.init_btn();
    _this.init_grid();
    sessionStorage.removeItem("scroll_top");
  },
    
  init_btn: function () {
    var _this = this;

    //查询按钮
    $(document).on('click', '#search_btn', function (e) {
      e.preventDefault();
      var query_param = $('#data_table').TableGrid('getparam');
      query_param.page = 1;

      $('#data_table').TableGrid('param', query_param);
      $('#data_table').TableGrid('reload');
    });
    
    //打开添加窗口
    $(document).on('click', '.load_add_modal_btn', function (e) {
      e.preventDefault();

      $('#submit_form').attr('action', _this.config.create_url);
      $('#submit_form')[0].reset();
      fillform( $('#submit_form') , _this.config.row );
      _this.set_password_line( _this.config.row.valid_type );
      
      $('#action_label').html('新增');
      $('#add_edit_modal').modal('show');
    });

    //打开编辑窗口
    $(document).on('click', '.edit_btn', function (e) {
      e.preventDefault();
      
      var index = $('.edit_btn').index( $(this) );
      var data = $('#data_table').TableGrid('getrow' , index);

      fillform( $('#submit_form') , data );
      _this.set_password_line( data.valid_type );
      $('#submit_form').attr('action', _this.config.update_url + '/' + data['id'] );
     
      $('#action_label').html('编辑');
      $('#add_edit_modal').modal('show');
    });

    //提交
    $(document).on('click', '#submit_btn', function (e) {
      e.preventDefault();

      var _thisbtn = $(this);
      var url = $('#submit_form').attr('action');
      var data = $('#submit_form').serializeObject();

      if (validform($('#submit_form'))) {
        data.event_type = _this.config.event_type ;
        data.event_id = _this.config.event_id ;
        $.post(url, data, function (ret) {
          tips(ret.info) ;
          if (ret.status) {
            $('#add_edit_modal').modal('hide');
            var query_param = $('#data_table').TableGrid('getparam');
            query_param.page = 1 ;
            $('#data_table').TableGrid('param' , query_param);
            $('#data_table').TableGrid('reload');
          }
        });
      }
    });
    
    //批量删除按钮
    $(document).on('click', '.del-btn', function (e) {
      e.preventDefault();
      var select_id = new Array();
      $("input[name='tablegrid-select[]']").each(function (index) {
        if ($(this).prop('checked')) {
          //取表数据
          var row = $('#data_table').TableGrid('getrow', index);
          select_id.push(row.id);
        }
      });

      if (select_id.length == 0) {
        tips('请选择数据');
      } else {
        _this.config.del_id = select_id.join(',');
        _this.del_data();
      }
    });

    //单个删除按钮
    $(document).on('click', '.del_one_btn', function (e) {
      e.preventDefault();
      var index = $('.del_one_btn').index( $(this) );
      var data = $('#data_table').TableGrid('getrow' , index);
      _this.config.del_id = data['id'];
      _this.del_data();
    });
    
    $(document).on('change' , 'input[name="valid_type"]' , function(e){
      var valid_type = $('input[name="valid_type"]:checked').val();
      _this.set_password_line( valid_type );
    });
    
    $(document).on('click' , '.sn_btn' , function(e){
      var url = _this.config.award_sn_url + $(this).data('id');
      window.location.href = url ;
    });
    
  },
  
  init_grid: function () {
    var _this = this;
    $('#data_table').TableGrid({
      url: _this.config.read_url,
      select_all: false,
      load_after_init: true,
      pagination : false ,
      param : { 
        event_type :  _this.config.event_type ,
        event_id : _this.config.event_id ,
      },
      row_style: function (row, index) {
        if (row.status == 1 ) {
          return 'info';
        }
      }
    });
  },
  
  set_password_line : function( valid_type ){
    if ( valid_type != 1 ) {
      $('#password_line').hide();
    } else {
      $('#password_line').show();
    }
  },
  
  del_data: function (ids) {
    var _this = this;

    sure.init('确定删除吗？', function () {
      var url = _this.config.delete_url;
      var data = {ids: _this.config.del_id};

      $.post(url, data, function (ret) {
        tips(ret.info);
        if (ret.status) {
          $('#data_table').TableGrid('reload');
        }
      });
    });
  },
};

var format_rank = function(value , row , index ){
  return Param.rank[value];
}

var format_amount = function(value , row , index ){
  return row.taken_qty + '/' + row.total_qty;
}
var format_take_rate = function(value , row , index ){
  return value == 0 ? '不限制' : value ;
}

var format_valid_type = function(value , row , index ){
  return Param.valid_type[value];
}

var opt_award_sn = function(value , row , index ){
  if ( row.valid_type == 2 ) {
    return '<a class="btn btn-info sn_btn" data-id="'+ row.id +'" href="#"><i class="fa fa-ticket"></i> 兑换码</a>';
  } else {
    return '';
  }
}


